<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class OrderDB extends BaseDB
{
    /**
     * 订单
     */

    public $tOrder = 'order';

    public function __construct()
    {
    }

    /**
     * 列表
     */
    public function getIndex()
    {
        $models = DB::table($this->tOrder)->where('del',0)->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $items = array();
        foreach ($models as $model) {
            $uname = $model->uname ? $model->uname : '-';
            $title = $model->title ? $model->title : '-';
            $money = $model->money / 100;
            $items[] = [
                'id'=>$model->id, 'uname'=>$uname,
                'title'=>$title, 'num'=>$model->num, 'money'=>$money,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 新增
     */
    public function setAdd($uname,$title,$num,$money)
    {
        $money = $money * 100;
        return DB::table($this->tOrder)->insertGetId([
            'uname'=>$uname, 'title'=>$title, 'num'=>$num,
            'money'=>$money, 'created_at'=>time(),
        ]);
    }

    /**
     * 列表
     */
    public function getIndexByUser($uwx)
    {
        $models = DB::table($this->tOrder)->where('del',0)->where('uname',$uwx)->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $items = array(); $models = $this->jsonToArr($models);
        foreach ($models as $model) {
            $uname = $model['uname'] ? $model['uname'] : '-';
            $title = $model['title'] ? $model['title'] : '-';
            $money = $model['money'] / 100;
            $items[] = [
                'id'=>$model['id'], 'uname'=>$uname,
                'title'=>$title, 'num'=>$model['num'], 'money'=>$money,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }
}